// 获取应用实例
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    isMember: true,
    showMask: false,
    maskType: "buyNow",
    address: [],
    area: "选择地址查看是否可以发货",
    checkListId1: 0,
    checkListId2: 0,
    currName1: "",
    currName2: "",
    checkNum: 1,
    scrollTop: 0,
    navBarHeight: app.globalData.navBarHeight,
    menuRight: app.globalData.menuRight,
    menuBotton: app.globalData.menuBotton,
    menuTop: app.globalData.menuTop,
    menuHeight: app.globalData.menuHeight,
    group: [
      {
        title: "颜色",
        list: [
          {
            id: 1,
            name: "冰玉白",
          },
          {
            id: 2,
            name: "深空灰",
          },
          {
            id: 3,
            name: "绝绝紫",
          },
          {
            id: 4,
            name: "深邃黑",
          },
        ],
      },
      {
        title: "规格",
        list: [
          {
            id: 1,
            name: "8+128",
          },
          {
            id: 2,
            name: "8+256",
          },
          {
            id: 3,
            name: "12+256",
          },
          {
            id: 4,
            name: "12+512",
          },
        ],
      },
    ],
  },

  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop,
    });
  },

  goBack() {
    wx.navigateBack({
      delta: 1,
    });
  },

  showSpec(e) {
    const that = this;
    const { type } = e.currentTarget.dataset;
    const { currName1, currName2 } = that.data;

    switch (type) {
      case "checkAddress":
        let address = wx.getStorageSync("address");
        this.setData({
          showMask: true,
          maskType: type,
          address,
        });
        break;
      case "buyNow":
        // if (currName1 != "" && currName2 != "") {
        //   wx.navigateTo({
        //     url: "/pages/order/create/create",
        //   });
        // } else {
        this.setData({
          showMask: true,
          maskType: type,
        });
        // }
        break;
      default:
        this.setData({
          showMask: true,
          maskType: type,
        });
        break;
    }
  },

  addMaskAddress() {
    wx.navigateTo({
      url: "/pages/handleAddress/handleAddress",
      success: (result) => { },
      fail: () => { },
      complete: () => { },
    });
    this.setData({
      showMask: false,
    });
  },

  closeMask() {
    this.setData({
      showMask: false,
      // checkListId1: 1,
      // checkListId2: 1,
      // currName1: "",
      // currName2: "",
      // checkNum: 1,
    });
  },

  previewImg() {
    wx.previewImage({
      current: "https://i.imgtg.com/2023/06/29/OkJC3B.png", //这里是前点击的查看的第一张图
      urls: ["https://i.imgtg.com/2023/06/29/OkJC3B.png"], //这里就是存档多图的数组放大可轮播
    });
  },

  checkList1(e) {
    const { id, name } = e.currentTarget.dataset;
    this.setData({
      checkListId1: id,
      currName1: name,
    });
  },
  checkList2(e) {
    const { id, name } = e.currentTarget.dataset;
    this.setData({
      checkListId2: id,
      currName2: name,
    });
  },

  stepper(e) {
    console.log(e.detail);
    this.setData({
      checkNum: e.detail,
    });
  },

  clickMaskBtn(e) {
    const that = this;
    const { maskbtn } = e.currentTarget.dataset;
    const { currName1, currName2 } = that.data;
    switch (maskbtn) {
      case "addCart":
        if (currName1 != "" && currName2 != "") {
          wx.showToast({
            title: "添加成功",
            icon: "success",
          });

          that.closeMask();
        } else {
          wx.showToast({
            title: "请选择",
            icon: "error",
          });
        }
        break;
      case "buyNow":
        if (currName1 != "" && currName2 != "") {
          wx.navigateTo({
            url: "/pages/order/create/create",
          });
          that.closeMask();
        } else {
          wx.showToast({
            title: "请选择",
            icon: "error",
          });
        }

        break;
      case "sureCheck":
        that.closeMask();
        break;

      default:
        break;
    }
  },

  sureAddress(e) {
    const { area } = e.currentTarget.dataset;
    console.log(area);
    this.setData({ area });
    this.closeMask();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { showmask } = options;
    if (showmask) {
      this.setData({ showMask: showmask, maskType: "addCart" });
    } else {
      this.setData({ showMask: false });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {};
  },
});
